package com.example.springbott.service;

import com.example.springbott.bean.MarketCategory;
import com.example.springbott.bean.MarketCategoryExample;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.silic.CategoryListVo;
import com.example.springbott.bean.silic.MarketCategoryVo;
import com.example.springbott.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商场管理——商品类目
 *
 * @author silic xia
 * @since 2022/11/21 18:01
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;

    /**
     * 商品类目List
     *
     * @param /null [id=pid]
     * @return data: commonData -- 商品类目名 -- 具体商品
     * errmsg: "成功"
     * errno: 0
     * @author silic xia
     * @since 2022/11/21 17:48
     */
    @Override
    public CommonData<MarketCategory> adminCategoryList() {

        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);

        for (MarketCategory marketCategory : marketCategories) {
            MarketCategoryExample example1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andPidEqualTo(marketCategory.getId());
            criteria.andDeletedEqualTo(false);
            List<MarketCategory> marketCategories1 = categoryMapper.selectByExample(example1);
            marketCategory.setChildren(marketCategories1);
        }

        PageHelper.startPage(1, marketCategories.size());
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(marketCategories);
        return CommonData.data(pageInfo);
    }

    /**
     * L1模块
     *
     * @param /null
     * @return data: {total: 10, pages: 1, limit: 10, page: 1,…}
     * errmsg: "成功"
     * errno: 0
     * @author silic xia
     * @since 2022/11/21 19:46
     */
    @Override
    public CommonData<CategoryListVo> adminCategoryL1() {

        String level = "L1";
        List<CategoryListVo> list = categoryMapper.selectCategoryL1(level);

        PageHelper.startPage(1, list.size());
        PageInfo<CategoryListVo> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 新增商品类目
     *
     * @param {"name":"大麻哈","keywords":"555","level":"L1","pid":0,"desc":"88888","iconUrl":"","picUrl":""}
     * @return {"errno":0,"data":{"id":1036045,"name":"大麻哈","keywords":"555","desc":"88888","pid":0,"iconUrl":"",
     * "picUrl":"","level":"L1","addTime":"2022-11-21 20:06:23","updateTime":"2022-11-21 20:06:23"},"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 20:06
     */
    @Override
    public MarketCategoryVo adminCategoryCreate(MarketCategory marketCategory) {
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        categoryMapper.insertSelective(marketCategory);
        MarketCategoryVo data = categoryMapper.selectCategoryById(marketCategory.getId());
        return data;
    }

    /**
     * 修改商品update——这里修改了一个老师案例的Bug
     * 【把一级类目修改为二级类目，父类目修改为当前类目，数据会被折叠消失】
     *
     * @param /children: [{id: 1036047, name: "拉伯", keywords: "阿丁", desc: "2222", iconUrl: "", picUrl: "", level: "L2"}]
     *                   desc: "88888"
     *                   iconUrl: ""
     *                   id: 1036045
     *                   keywords: "6666"
     *                   level: "L1"
     *                   name: "大麻哈"
     *                   picUrl: ""
     * @return {"errno":0,"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 20:36
     */
    @Override
    public void adminCategoryUpdate(MarketCategory marketCategory) {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketCategory.getId());
        marketCategory.setUpdateTime(new Date());

        categoryMapper.updateByExampleSelective(marketCategory, example);
    }

    /**
     * 商品类目——delete
     *
     * @param
     * {"id":1036056,"name":"嗡嗡嗡嗡嗡嗡","keywords":"111",
     * "desc":"111","iconUrl":"","picUrl":"","level":"L1","children":[]}
     * @return {"errno":0,"errmsg":"成功"}
     *
     * @author  silic xia
     * @since 2022/11/21 21:20
     */
    @Override
    public void adminCategoryDelete(MarketCategory marketCategory) {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketCategory.getId());
        marketCategory.setUpdateTime(new Date());
        marketCategory.setDeleted(true);
        categoryMapper.updateByExampleSelective(marketCategory,example);
    }
}

